package com.ray.util;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class ErWeiMaUtil {
	
	public static String getImagebase64(String imgFileName) {  
        byte[] data = null;  
        try {  
            InputStream in = new FileInputStream(imgFileName);  
            data = new byte[in.available()];  
            in.read(data);  
            in.close();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
          
        BASE64Encoder encoder = new BASE64Encoder();  
        return encoder.encode(data);  
    }  
  
    //Base64解码并保存图片文件  
    public static  void saveImage(String base64, String imgFileName) {  
        BASE64Decoder decoder = new BASE64Decoder();  
        try {  
            byte[] bytes = decoder.decodeBuffer(base64);  
            for (int i = 0; i < bytes.length; ++i) {  
                if (bytes[i] < 0) {  
                    bytes[i] += 256;  
                }  
            }  
            OutputStream out = new FileOutputStream(imgFileName);  
            out.write(bytes);  
            out.flush();  
            out.close();  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }
}
